package dataAccessLayer;

import java.util.List;

import org.hibernate.Query;

import connection.Connector;
import entities.Category;
import entities.Picture;
import entities.User;

public class DataGetter extends Connector{
	public DataGetter(){
		super();
	}
	public User getUserByName(String userName) {
		Query query = session
				.createQuery("from User u where u.UserName=:userName");
		query.setString("userName", userName);
		query.setMaxResults(1);
		User user = (User) query.uniqueResult();
		if (user != null) {
			return user;
		}
		return null;
	}

	public Category getCategoryByName(String categoryName) {
		Query query = session
				.createQuery("from Category c where c.CategoryName=:categoryName");
		query.setString("categoryName", categoryName);
		query.setMaxResults(1);
		Category category = (Category) query.uniqueResult();
		if (category != null) {
			return category;
		}
		return null;
	}

	public Picture getPictureByName(String pictureName) {
		Query query = session
				.createQuery("from Picture p where p.PictureName=:pictureName");
		query.setString("pictureName", pictureName);
		query.setMaxResults(1);
		Picture picture = (Picture) query.uniqueResult();
		if (picture != null) {
			return picture;
		}
		return null;
	}

	public List<Picture> getAllPictures() {
		Query query = session.createQuery("from Picture");
		List<Picture> pictures = query.list();
		if (pictures != null) {
			return pictures;
		} else {
			return null;
		}
	}
	
	public List<Category> getAllSubcategories(String parentName) {
		Query query = session.createQuery("from Category c where c.Parent.CategoryName=:parentName");
		query.setString("parentName", parentName);
		List<Category> categories = query.list();
		if (categories != null) {
			return categories;
		} else {
			return null;
		}
	}
	public List<User> getAllUsers(){
		Query query=session.createQuery("from User");
		List<User> users=query.list();
		return users;
	}
	public List<Picture> getAllUserPictures(String userName) {
		Query query = session
				.createQuery("from Picture p where p.Category.User.UserName =:username");
		query.setString("username", userName);
		List<Picture> pictures = query.list();
		if (pictures != null) {
			return pictures;
		} else {
			return null;
		}
	}

	public List<Picture> getPicturesByCategory(String categoryName) {
		Query query = session
				.createQuery("from Picture p where p.Category.CategoryName=:categoryName");
		query.setString("categoryName", categoryName);
		List<Picture> pictures = query.list();
		if (pictures != null) {
			return pictures;
		} else {
			return null;
		}
	}

	public List<Category> getCategoriesByUser(String userName) {
		Query query = session
				.createQuery("from Category c where c.User.UserName=:userName");
		query.setString("userName", userName);
		List<Category> categories = query.list();
		if (categories != null) {
			return categories;
		} else {
			return null;
		}
	}
}
